#!/usr/bin/env python
#

"""Utility to quickly deploy portable apps to all computers in LAN.

This version works on a windows machine only.
"""

__version__ = "$Revision$"
# Last Modified on $Date$
# by $Author$
# from $Source$

import os
import os.path
import shutil
import logging
import sys
import string
import glob
import subprocess

from mutils import log

this_comp = "\\\\MCA12\\"

# All computers in LAN where you want to deploy your apps.
dest_comps = [ "\\\\MCA20\\",
               "\\\\MCA26\\",
               "\\\\MCA36\\",
               "\\\\MCA-39\\",
               "\\\\MCA-48\\",
               "\\\\MCA-76\\",
               "\\\\MCA004\\",
               "\\\\MCA0041\\",
               "\\\\MCA0130\\",
               "\\\\MCA0140\\",
               "\\\\MCA051\\",
               "\\\\MCA055\\",
               "\\\\MCA10\\",
               "\\\\MCA101\\",
               "\\\\MCA102\\",
               "\\\\MCA104\\",
               "\\\\MCA105\\",
               "\\\\MCA106\\",
               "\\\\MCA107\\",
               "\\\\MCA109\\",
               "\\\\MCA111\\",
               "\\\\MCA113\\",
               "\\\\MCA114\\",
               "\\\\MCA115\\",
               "\\\\MCA117\\",
               "\\\\MCA119\\",
               "\\\\MCA122\\",
               "\\\\MCA123\\",
               "\\\\MCA124\\",
               "\\\\MCA125\\",
               "\\\\MCA127\\",
               "\\\\MCA128\\",
               "\\\\MCA129\\",
               "\\\\MCA130\\",
               "\\\\MCA132\\",
               "\\\\MCA133\\",
               "\\\\MCA134\\",
               "\\\\MCA135\\",
               "\\\\MCA136\\",
               "\\\\MCA138\\",
               "\\\\MCA139\\",
               "\\\\MCA14\\",
               "\\\\MCA141\\",
               "\\\\MCA144\\",
               "\\\\MCA146\\",
               "\\\\MCA148\\",
               "\\\\MCA149\\",
               "\\\\MCA150\\",
               "\\\\MCA16\\",
               "\\\\MCA18\\",
               "\\\\MCA2\\",
               "\\\\MCA20\\",
               "\\\\MCA22\\",
               "\\\\MCA23\\",
               "\\\\MCA24\\",
               "\\\\MCA25\\",
               "\\\\MCA26\\",
               "\\\\MCA27\\",
               "\\\\MCA29\\",
               "\\\\MCA30\\",
               "\\\\MCA31\\",
               "\\\\MCA32\\",
               "\\\\MCA33\\",
               "\\\\MCA35\\",
               "\\\\MCA36\\",
               "\\\\MCA51\\",
               "\\\\MCA52\\",
               "\\\\MCA53\\",
               "\\\\MCA54\\",
               "\\\\MCA56\\",
               "\\\\MCA57\\",
               "\\\\MCA58\\",
               "\\\\MCA60\\",
               "\\\\MCA61\\",
               "\\\\MCA62\\",
               "\\\\MCA63\\",
               "\\\\MCA64\\",
               "\\\\MCA65\\",
               "\\\\MCA66\\",
               "\\\\MCA67\\",
               "\\\\MCA68\\",
               "\\\\MCA69\\",
               "\\\\MCA70\\",
               "\\\\MCA72\\",
               "\\\\MCA73\\",
               "\\\\MCA74\\",
             ]


#The entries in this list should not end with slash.
src_dirs = [ "C:\\Program Files\\FirefoxPortable",
             "C:\\Program Files\\7-Zip",
             "C:\\Program Files\\Foxit PDF Reader",
           ]

src_files = [ "C:\\Documents and Settings\\All Users\\Desktop\\Firefox Web Browser.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Firefox Web Browser.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Firefox Web Browser.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\7-Zip.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Foxit PDF Reader.lnk",
            ]

users_list = "C:\\Documents and Settings\\"
quick_launch_list = [ "C:\\Documents and Settings\\Default User\\Application Data\\Microsoft\\Internet Explorer\\Quick Launch\\Firefox Web Browser.lnk",
                    ]

del_search_list = [ "*firefox*",
                  ]
del_search_dirs = [ "C:\\Documents and Settings\\*\\Desktop\\",
                    "C:\\Documents and Settings\\*\\Start Menu\\Programs\\",
                    "C:\\Documents and Settings\\*\\Application Data\\Microsoft\\Internet Explorer\\Quick Launch\\",
                  ]

xcopy_cmd = 'XCOPY /D /E /R /Y /V /C /Q /H /K /I'




def deploy(dest_comp):

    for src_dir in src_dirs:
        try:
            if not os.path.exists(src_dir):
                log.critical('Source dir %s does not exist' % src_dir)
                sys.exit(1)

            log.info('Deploying ' + src_dir)
            dest_dir = os.path.join(dest_comp, src_dir.replace('C:','C$'))

            #The following operation is slow for big directories.
            if os.path.exists(dest_dir):
                shutil.rmtree(dest_dir)
            shutil.copytree(src_dir, dest_dir)

            #Using window's xcopy.
            #I know, it's ugly.
            #retcode = subprocess.call('%s "%s" "%s"' % (xcopy_cmd, src_dir, dest_dir), shell=True)
            #if retcode:
            #    log.error('xcopy command returned error while copying "%s" to "%s"' % (src_dir, dest_dir))

        except:
            log.error( 'Unexpected error while deploying %s : %s ' % (src_dir, sys.exc_info()[1]) )


def delete_shortcuts(dest_comp):
    log.debug('Deleting old data')
    for a in del_search_list:
        for b in del_search_dirs:
            for c in glob.glob( dest_comp + b.replace('C:','C$') + a ):
                try:
                    if os.path.isfile(c):
                        log.debug('Deleting File ' + c)
                        os.remove(c)
                        #fdelete(c)
                        #log.debug('Will delete' + c)
                except:
                    log.error( 'Unexpected error while deleting %s : %s' % (c, sys.exc_info()[1]) )

def create_shortcuts(dest_comp):

    try:

        log.debug('Creating shortcuts')
        for src_file in src_files:

            if not os.path.exists(src_file):
                log.critical('Source file %s does not exist' % src_file)
                sys.exit(1)

            dest_file = os.path.join(dest_comp, src_file.replace('C:','C$'))
            log.debug('Creating file %s' % dest_file)

            dir_name = os.path.dirname(dest_file)

            if not os.path.exists(dir_name):
                log.debug('Creating dir %s' % dir_name)
                os.makedirs(dir_name)

            shutil.copy2(src_file, dest_file)
    except:
        log.error( 'Unexpected error: %s' % sys.exc_info()[1] )


def create_quick_launch(dest_comp):

    try:
        log.debug('Creating quick launch shortcuts')

        users = os.path.join(dest_comp, users_list.replace('C:','C$'))
        for user in os.listdir(users):

            for link in quick_launch_list:

                if not os.path.exists(link):
                    log.critical('Source file %s does not exist' % link)
                    sys.exit(1)

                l = link.replace("Default User", user)
                dest_file = os.path.join(dest_comp, l.replace('C:','C$'))
                log.debug('Creating file %s' % dest_file)

                dir_name = os.path.dirname(dest_file)

                if not os.path.exists(dir_name):
                    log.debug('Creating dir %s' % dir_name)
                    os.makedirs(dir_name)

                shutil.copy2(link, dest_file)
    except:
        log.error( 'Unexpected error: %s' % sys.exc_info()[1] )


def main():

    for dest_comp in dest_comps:
        log.info('\n\n -----------------------------------------------------')

        if dest_comp == this_comp:
            continue

        if not os.path.exists(dest_comp + 'C$'):
            log.info('Can not reach computer %s' % dest_comp)
            continue

        log.info('Started deploying to computer ' + dest_comp)

        deploy(dest_comp)
        delete_shortcuts(dest_comp)
        create_shortcuts(dest_comp)
        create_quick_launch(dest_comp)

        log.info('Finished deploying to computer ' + dest_comp)



if __name__ == '__main__':
    sys.exit(main())
